import {
    CATEGORY_DISABLE,
    SELECT_SUBREDDIT,

    SELECT_ALL,
    SELECT_ITEM,

    REMOVE_REQUEST_CATEGOERY,
    REMOVED_CATEGOERY,
    OPEN_MODAL,
    CLOSE_MODAL,
    INVALIDATE_SUBREDDIT,
    REQUEST_POSTS, RECEIVE_POSTS
} from '../actionsType/news';

export function category(state = {enable_close: 0}, action) {
    switch (action.type) {
        case CATEGORY_DISABLE:
            console.log(action)
            return Object.assign({},
                ...state,
                {
                    enable_close: action.all ? action.num : (state.enable_close + action.num)
                }
            );
        default:
            return state;
    }
}

export function selectedSubreddit(state = 'reactjs', action) {
    switch (action.type) {
        case SELECT_SUBREDDIT:
            return action.subreddit
        default:
            return state
    }
}

function posts(state = {
    isFetching: false,
    didInvalidate: false,
    data: []
}, action) {
    switch (action.type) {
        case INVALIDATE_SUBREDDIT:
            return Object.assign({}, state, {
                didInvalidate: true
            });
        case REQUEST_POSTS:
            return Object.assign({}, state, {
                isFetching: true,
                didInvalidate: false
            });
        case RECEIVE_POSTS:
            return Object.assign({}, state, {
                isFetching: false,
                didInvalidate: false,
                // items: action.data,
                lastUpdated: action.receivedAt,
                ...action.data
            });
        default:
            return state
    }
}

export function news(state = {
    data: {data: []},
    modal: {
        open: false,
        autoHeight: false
    },
    keyword: '',
}, action) {
    switch (action.type) {
        case INVALIDATE_SUBREDDIT:
        case SELECT_ALL:
            return Object.assign({}, state, {
                data: {
                    data: state.data.data.map((item)=> {
                        item.checked = action.flag;
                        return item;
                    })
                }
            });
        case SELECT_ITEM:
            return Object.assign({}, state, {
                data: {
                    data: [
                        ...state.data.data.slice(0, action.index),
                        _.extend({}, state.data.data[action.index], {
                            checked: action.checked
                        }),
                        ...state.data.data.slice(action.index + 1)
                    ]
                }
            });
        case CLOSE_MODAL:
            return Object.assign({}, state, {modal: {open: false}});
        case OPEN_MODAL:
            return Object.assign({}, state, {modal: {open: true, autoHeight: true}})

        case REMOVE_REQUEST_CATEGOERY:
        case REMOVED_CATEGOERY:
            const tm = [];
            if (action.classifyids.indexOf(',') > -1) {
                state.data.data.map((item)=> {
                    !item.checked && tm.push(item);
                });
            } else {
                return Object.assign({}, state, {
                    data: {
                        data: [
                            ...state.data.data.slice(0, action.index),
                            ...state.data.data.slice(action.index + 1)
                        ]
                    }
                });
            }
            return Object.assign({}, state, {
                data: {
                    data: tm
                }
            });
        case RECEIVE_POSTS:
        case REQUEST_POSTS:
            return Object.assign({}, state, {
                data: posts(state[action.subreddit], action)
            });
        default:
            return state
    }
}

